package cn.smxy.admin.service.impl;

import cn.smxy.admin.constant.AdminConstant;
import cn.smxy.admin.mapper.AdminMapper;
import cn.smxy.admin.param.AdminUserParam;
import cn.smxy.admin.pojo.AdminUser;
import cn.smxy.admin.pojo.PieData;
import cn.smxy.admin.service.AdminService;
import cn.smxy.admin.excel.AdminUserExcel;
import cn.smxy.admin.vo.EchartsVo;
import cn.smxy.admin.vo.PieVo;
import cn.smxy.client.CategoryClient;
import cn.smxy.client.CommentClient;
import cn.smxy.client.PlantClient;
import cn.smxy.dto.CommentCountDto;
import cn.smxy.param.PageParam;
import cn.smxy.pojo.Category;
import cn.smxy.util.MD5Util;
import cn.smxy.util.R;
import cn.smxy.vo.PlantVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.*;

@Service
@Slf4j
public class AdminServiceImpl extends ServiceImpl<AdminMapper, AdminUser> implements AdminService {

    @Autowired
    private CategoryClient categoryClient;
    @Autowired
    private PlantClient plantClient;
    @Autowired
    private CommentClient commentClient;


    /**
     * excel读出数据存入数据库
     * 1.密码加盐加密
     * 2.存入数据库
     *
     * @param list excel表中解析出的集合
     */
    @Override
    public void saveExcel(List<AdminUser> list) {
        for (AdminUser adminUser : list) {
            String newPwd = MD5Util.encode(AdminConstant.USER_SLAT + adminUser.getAdminPassword());
            adminUser.setAdminPassword(newPwd);
            adminUser.setCreateTime(System.currentTimeMillis());
            adminUser.setAdminRole(AdminConstant.LEVEL_ALL);
        }
        this.saveBatch(list);
    }

    /**
     * 获取各作品评论数
     *
     * @return
     */
    @Override
    public R pie() {
        PieVo pieVo = new PieVo();

        List<CommentCountDto> list = commentClient.getCount();
        String[] worksList = new String[list.size()];
        PieData[] pieDataArr = new PieData[list.size()];
        for (int i = 0; i < list.size(); i++) {
            PlantVo info = plantClient.info(list.get(i).getPlantId());
            String plantName = info.getPlantName();
            worksList[i] = plantName;
            PieData pieData = new PieData();
            pieData.setName(plantName);
            pieData.setValue(list.get(i).getCount());
            pieDataArr[i] = pieData;
        }

        pieVo.setWorksList(worksList);
        pieVo.setPieData(pieDataArr);

        return R.ok(null,pieVo);
    }

    /**
     * 数据可视化测试
     *
     * @return
     */
    @Override
    public R echarts() {
        EchartsVo echartsVo = new EchartsVo();

        List<Category> list = categoryClient.list();
        String[] categoryList = new String[list.size()];
        for (int i = 0; i < list.size(); i++) {
            categoryList[i] = list.get(i).getCategoryName();
        }
        int[] countList = plantClient.getCount(list);

        echartsVo.setCategoryList(categoryList);
        echartsVo.setCountList(countList);
        return R.ok(null,echartsVo);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @Override
    public R removeBatch(Long[] ids) {
        boolean b = this.removeBatchByIds(Arrays.asList(ids));
        if(!b) {
            log.info("AdminServiceImpl.removeBatch业务结束，结果为:{}","删除失败");
            return R.ok("删除失败");
        }
        log.info("AdminServiceImpl.removeBatch业务结束，结果为:{}","删除成功");
        return R.fail("删除成功");
    }

    /**
     * 分页查询管理员列表
     *
     * @param pageParam
     * @return
     */
    @Override
    public Object list(PageParam pageParam) {
        // 参数拆分
        int currentPage = pageParam.getCurrentPage();
        int pageSize = pageParam.getPageSize();
        // 设置分页属性
        IPage<AdminUser> page = new Page<>(currentPage, pageSize);
        page = baseMapper.selectPage(page, null);
        // 结果封装
        long total = page.getTotal();
        List<AdminUser> records = page.getRecords();
        R ok = R.ok("查询成功", records, total);
        log.info("AdminServiceImpl.list业务结束，结果为:{}",ok);
        return ok;
    }

    /**
     * 管理员登录
     * 1.密码加密加盐
     * 2.数据库比对账号密码
     *
     * @param adminUserParam
     * @return
     */
    @Override
    public R login(AdminUserParam adminUserParam) {
        // 密码加密加盐
        String newPwd = MD5Util.encode(AdminConstant.USER_SLAT + adminUserParam.getAdminPassword());
        // 条件封装
        QueryWrapper<AdminUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("admin_account",adminUserParam.getAdminAccount());
        queryWrapper.eq("admin_password",newPwd);
        // 数据库查询
        AdminUser adminUser = baseMapper.selectOne(queryWrapper);
        if(adminUser == null) {
            log.info("AdminServiceImpl.login业务结束，结果为:{}","账号或密码错误");
            return R.fail("账号或密码错误");
        }
        R ok = R.ok("登录成功",adminUser);
        log.info("AdminServiceImpl.login业务结束，结果为:{}",ok);
        return ok;
    }

    /**
     * 导出所有管理员数据
     *
     * @return
     */
    @Override
    public List<AdminUserExcel> getAll() {
        List<AdminUser> list = this.list();
        List<AdminUserExcel> result = new ArrayList<>();
        for (AdminUser adminUser : list) {
            result.add(new AdminUserExcel(adminUser));
        }
        return result;
    }

}
